import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {Link} from 'react-router-dom';
import {PAGINATION_OPTIONS, TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT, FIXED_COLUMN_WIDTH} from '../constants';
import {PERMISSION} from './constants';
import {injectIntl} from './intl';
import {formatDateTime} from '../utils';
import {hasPermissions} from './utils';
import DropdownMenu from 'Shared/components/DropdownMenu';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }

    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };

    render() {
        const colums = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dealer',
                    defaultMessage: '总代'
                }),
                dataIndex: 'dealerCode',
                render: (text, record) => `(${record.dealerCode})${record.dealerName}`
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.subDealerCode',
                    defaultMessage: '经销商'
                }),
                dataIndex: 'subDealerCode',
                render: (text, record) => `(${record.subDealerCode})${record.subDealerName}`
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.maintenanceNumber',
                    defaultMessage: '保养次数'
                }),
                dataIndex: 'maintenanceNumber',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'maintenanceNumber' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.vin',
                    defaultMessage: 'VIN'
                }),
                dataIndex: 'vin',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'vin' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.salesDate',
                    defaultMessage: '实销日期'
                }),
                dataIndex: 'salesDate',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'salesDate' && this.props.sortedOrder,
                render: text => formatDateTime(text, DATETIME_FORMAT)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.mileage',
                    defaultMessage: '行驶里程'
                }),
                dataIndex: 'mileage',
            }];
        colums.push({
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.operation',
                defaultMessage: '操作'
            }),
            width: FIXED_COLUMN_WIDTH,
            fixed: 'right',
            render: (text, r) => {
                const menus = [{
                    id: PERMISSION.detail,
                    children: <Link key="detail" to={routes.detail.format(r.id)}>{
                        this.props.intl.formatMessage({
                            id: 'tablePanel.detail',
                            defaultMessage: '详情'
                        })
                    }</Link>,
                }];
                return <DropdownMenu key={r.id} menus={menus} primaryLength={1} id={r.id} />;
            }
        });
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={colums}
                    dataSource={this.props.list}
                    onChange={this.onChangeSort}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    locale={locale}
                    {...TABLE} />
            </div>
        );
    }
}

TablePanel.propTypes = {
    errorMessage: PropTypes.string.isRequired,
    list: PropTypes.array.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    refreshListData: PropTypes.func,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    list: getData(state),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder'])
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
